package com.fun.wyunfei.timeline.adapter;

import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Toast;

import com.fun.wyunfei.timeline.MainActivity;
import com.fun.wyunfei.timeline.R;
import com.fun.wyunfei.timeline.bean.Article;
import com.fun.wyunfei.timeline.service.DbService;
import com.fun.wyunfei.timeline.viewholder.ViewTimePicker;

import java.util.List;

/**
 * create by author vvyunfei on 2018/11/14 16
 */
public class TimeLineAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    private List<Article> list;
    private Runnable clickDate;
    private Context context;

    public TimeLineAdapter(Context context, List<Article> list, Runnable clickDate) {
        // LayoutInflater.from(MainActivity.this)
        this.context = context;
        this.mInflater = LayoutInflater.from(context);
        this.list = list;
        this.clickDate = clickDate;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, android.view.View convertView, ViewGroup parent) {
        ViewTimePicker viewHolder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_timeline, parent, false);
            viewHolder = new ViewTimePicker(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewTimePicker) convertView.getTag();
        }
        Article article = list.get(position);
        viewHolder.id = article.getId();
        viewHolder.tvDate.setText(article.getDate());
        viewHolder.tvTitle.setText(article.getTitle());
        viewHolder.tvContent.setText(article.getContent());
        viewHolder.tvOther.setText(article.getOther());

        viewHolder.tvDate.setOnClickListener(view -> clickDate.run());

        viewHolder.timeLineCard.setOnLongClickListener((view) -> {
//            Toast.makeText(context, "you long click a item", Toast.LENGTH_SHORT).show();
            Snackbar snackbar = Snackbar.make(view, "是否删除该项", Snackbar.LENGTH_LONG)
                    .setAction("删除", viewDelete -> {
                        new DbService(context).deleteById(article.getId());
                        Toast.makeText(context, "删除成功", Toast.LENGTH_SHORT).show();
                        MainActivity mainActivity = (MainActivity) context;
                        mainActivity.refreshList();
                    });
            View v = snackbar.getView();
            ViewGroup.LayoutParams vl = v.getLayoutParams();
            CoordinatorLayout.LayoutParams cl = new CoordinatorLayout.LayoutParams(vl.width, vl.height);
            //设置显示位置
            cl.gravity = Gravity.CENTER;
            v.setLayoutParams(cl);
            snackbar.show();
            return false;
        });

        return convertView;
    }
}
